package com.wy17113.controller.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.wy17113.model.entity.ScoreRecord;
import com.wy17113.model.service.IUserService;

public class AjaxCheckQianDaoServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("text/xml; charset=GBK");
		PrintWriter out = response.getWriter();

		SimpleDateFormat smt = new SimpleDateFormat("yyyy-MM-dd");
		
		WebApplicationContext webContext = WebApplicationContextUtils
				.getRequiredWebApplicationContext(request.getSession()
						.getServletContext());
		String id = request.getParameter("id");
		IUserService users = (IUserService) webContext.getBean("userService");
				
		int flag = users.checkUserIsQianDao(id, smt.format(new Date()));
		if(flag == 0){
			ScoreRecord sr = new ScoreRecord();
		
			sr.setRecordDate(smt.format(new Date()));
			sr.setRecordNum(5);
			sr.setState(-1);
			sr.setUserId(id);
			sr.setScoreType(1);
			
			users.checkQianDao(sr);		
			int scoresNum = users.getUserScoresById(id);
			out.print(scoresNum);
		}else{
			out.print("flase");
		}		
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}
}